package com.xiaozhijun;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class P_20211130 {
    public static void main(String[] args) {
        System.out.println(unzipString("a(b(c)<3>d)<2>e"));
        System.out.println(unzipString("abc(d)<2>e"));
        System.out.println(unzipString("ab3c(d)<2>e"));
    }

    public static String unzipString(String records) {
        // abc(d)<2>e -> abcdde
        // a(b(c)<3>d)<2>e -> abcccdbcccde
        Stack<String> stack = new Stack<>(); // 存储的是临时变量
        int num = 0;
        StringBuilder curr = new StringBuilder(); // 最终计算结果
        String flag = "";
        for (char c : records.toCharArray()) {
            if (c != '(' && c != ')' && c != '>') {
                if (c == '<') {
                    flag = String.valueOf('<');
                    continue;
                }
                if (Character.isDigit(c) && flag.equals("<")) {
                    num = Character.getNumericValue(c); // char 转 int
                    flag = "";
                    continue;
                }
                curr.append(c);
                continue;
            }
            if (c == '(') {
                stack.push(curr.toString());
                curr = new StringBuilder();
                continue;
            }
            if (c == '>') {
                String temp = curr.toString();
                curr = new StringBuilder();
                for (int i = 0; i < num; i++) {
                    curr.append(temp);
                }
                temp = curr.toString();
                curr = new StringBuilder();
                curr.append(stack.pop()).append(temp);
            }
        }
        return curr.toString();
    }
}
